package com.pc.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pc.entity.Food;
import com.pc.mapper.FoodMapper;
import com.pc.utils.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class FoodService {
    @Resource
    private FoodMapper foodMapper;

    //根据查询结果显示菜品
    public Result getCategoryFood(String category){
        QueryWrapper<Food> wrapper = new QueryWrapper<>();
        wrapper.eq("category", category);
        List<Food> foods = foodMapper.selectList(wrapper);
        if(foods.size()>0)
            return Result.success("查询菜品成功",foods);
        else
            return Result.fail("获取菜品失败");
    }
    //搜索功能
    public String getCategory(String foodname){
        QueryWrapper<Food> wrapper = new QueryWrapper<>();
        wrapper.eq("name",foodname);
        Food food = foodMapper.selectOne(wrapper);
        if(food==null)
            return "没有查询到";
        else
            return food.getCategory();
    }

    //显示全部菜品
    public List<Food> getAllFood(){
        QueryWrapper<Food> wrapper = new QueryWrapper<>();
        return foodMapper.selectList(wrapper);
    }

    //更改菜品价格
    public boolean modifyFoodPrice(int id,int price){
        QueryWrapper<Food> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        Food food = new Food();
        food.setPrice(price);
        int update = foodMapper.update(food, wrapper);
        return update!=0;
    }
    //查询某个商品Pc端
    public List<Food> getFoodByname(String foodname){
        QueryWrapper<Food> wrapper = new QueryWrapper<>();
        wrapper.eq("name",foodname);
        return foodMapper.selectList(wrapper);
    }

    //菜品打九折
    public boolean CostFoodPrice( ){
        QueryWrapper<Food> wrapper = new QueryWrapper<>();
        int[] arr = {4,35,3,8,11,15,5,12,11,
                10,16,4,5,5,4,1888,495,239,
                295,80,120,120,120,85,120,20,45,
                10,15,4,300,350,400,559,673,20,
                50,60,30,40,188,298,450,239,89,
                450};
        int update=0;
        Food food = new Food();
        for (int id = 0; id <46 ; id++) {
            wrapper.eq("id",id);
            food.setId(id);
            int price= (int) (arr[id]*0.9);
            food.setPrice(price);
            update=foodMapper.updateById(food);
        }
        return update!=1;
    }
    //重置菜单价格
    public boolean ResetFoodPrice( ){
        QueryWrapper<Food> wrapper = new QueryWrapper<>();
        int[] arr = {4,35,3,8,11,15,5,12,11,
                10,16,4,5,5,4,1888,495,239,
                295,80,120,120,120,85,120,20,45,
                10,15,4,300,350,400,559,673,20,
                50,60,30,40,188,298,450,239,89,
                450};

        int update=0;
        Food food = new Food();
        for (int id = 0; id <46 ; id++) {
            wrapper.eq("id",id);
            food.setId(id);
            food.setPrice(arr[id]);

            update=foodMapper.updateById(food);

        }

        return update!=1;
    }
}


